package com.design.twords.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.design.twords.MainActivity;
import com.design.twords.R;
import com.design.twords.config.ConfigData;
import com.design.twords.database.User;
import com.design.twords.database.UserData;
import com.design.twords.utils.MyApplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Create by Alen
 * Time:  13:31
 */
public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "LoginActivity";

    private String username,password;
    private SharedPreferences spre;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        MyApplication.getInstance().addActivity(this);

        Button bt_login = findViewById(R.id.bt_login);
        EditText et_username = findViewById(R.id.et_username);
        EditText et_password = findViewById(R.id.et_password);
        TextView tv_register = findViewById(R.id.tv_register);

        final Intent intent = new Intent(LoginActivity.this, MainActivity.class);

        spre = getSharedPreferences("user_details",MODE_PRIVATE);
        if(spre.contains("username") && spre.contains("password")){
            startActivity(intent);
            finish();
        }
        //登录
        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = et_username.getText().toString().trim();
                password = et_password.getText().toString();
                User user=new User();
                user.setUsername(username);
                user.setPassword(password);
                user.login(new SaveListener<User>() {
                    @Override
                    public void done(User user, BmobException e) {
                        if(e==null){
                            SharedPreferences.Editor editor = spre.edit();
                            editor.putString("username",username);
                            editor.putString("password",password);
                            editor.apply();

                            ConfigData.setUserId(user.getObjectId());
                            ConfigData.setNickName(user.getNickName());
                            Log.d(TAG,user.getNickName());

                            UserData userData = new UserData();
                            userData.setUserId(user.getObjectId());
                            userData.setNickName(user.getNickName());
                            userData.save();

                            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
                            startActivity(intent);
                            finish();
                        }
                        else{
                            Toast.makeText(LoginActivity.this, "登录失败:网络出了点小问题", Toast.LENGTH_LONG).show();
                            Log.i("Logintag",e.getMessage());
                        }
                    }
                });


            }
        });
        //注册
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));

            }
        });



    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.getInstance().removeActivity(this);
    }
}
